<?php

include_once('../interfaces/IDao.php');
include_once('../conexao.php');

class LoginDao implements IDao {

    public $con;
    private $conexao;

    public function __construct() {
        try {
            $this->con = new conexao();
            $this->conexao = $this->con->getConnection();
            //echo "con ok!"; 
        } catch (PDOException $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function add($login) {
        $rs = $this->conexao->prepare("INSERT INTO proj_helios_v1.login (login, passwd, usuario_id, ativo) values (?, ?, ?, ?)");
        $logon = $login->getLogin();
        $passwd = $login->getPasswd();
        $usuario_id = $login->getUsuario_id();
        $ativo = $login->getAtivo();

        $rs->bindParam(1, $logon, PDO::PARAM_STR);
        $rs->bindParam(2, $passwd, PDO::PARAM_STR);
        $rs->bindParam(3, $usuario_id, PDO::PARAM_STR);
        $rs->bindParam(4, $ativo, PDO::PARAM_STR);

        if ($rs->execute()) {
            return true;
        }
    }

    public function auth($login) {
        try {
            $logon = $login->getEmail();
            $senha = $login->getPasswd();
            $rs = $this->conexao->prepare("SELECT * FROM proj_helios_v1.login where email = ? AND passwd = ?");
            $rs->bindParam(1, $logon, PDO::PARAM_STR);
            $rs->bindParam(2, $senha, PDO::PARAM_STR);
            if ($rs->execute()) {
                echo "Parte 1 ok";

                if ($rs->rowCount() == 1) {

                    while ($row = $rs->fetch(PDO::FETCH_OBJ)) {
                        echo "Parte 2 ok";
                        if ($row->ativo == 1) {
                            echo "parte 3 ok";
                            $usuario_id = $row->usuario_id;

                            //echo $usuario_id;
                            $rs = $this->conexao->prepare("SELECT * FROM proj_helios_v1.usuario where id = ?");
                            $rs->bindParam(1, $usuario_id, PDO::PARAM_INT);
                            if ($rs->execute()) {
                                echo "parte 4 ok";
                                if ($rs->rowCount() == 1) {

                                    session_start();
                                    while ($row = $rs->fetch(PDO::FETCH_OBJ)) {
                                        echo "parte 5 ok";
                                        $_SESSION['usuario'] = $row->nome;
                                    }

                                    return true;
                                } else {
                                    return false;
                                }
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            }
        } catch (Exception $exc) {
            echo $exc->getTraceAsString() . "Autenticação com problemas! ;x";
        }
    }

    public function delete($model) {
        $id = $model->getId();
        $rs = $this->conexao->prepare("DELETE FROM login WHERE id = ?");
        $rs->bindParam(1, $id, PDO::PARAM_INT);

        if ($rs->execute()) {
            return true;
        }
    }

    public function edit($model) {
        //Edit pendente. 

        /* if (!empty($model->getLogin())) {
          $logon = $model->getLogin();
          }else if (!empty ($model->getSenha())){
          $passwd = $model->getSenha();
          }

          try {
          //$rs = $this->conexao->prepare("UPDATE login SET    ");

          } catch (PDOException $exc) {
          echo $exc->getTraceAsString();
          }

          } */
        try {
            
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function select($model) {
        
    }

}

?>
